﻿using System;
using System.Collections.Generic;
using System.Text;
using SevenZip;
using System.IO;

namespace CommonUtil
{
    public class CompressHelper
    {
        private static string currentPath="";
        static CompressHelper()
        {
            currentPath = AppDomain.CurrentDomain.BaseDirectory;
            SevenZipCompressor.SetLibraryPath(string.Format(@"{0}7z.dll", currentPath));            
        }

        public static void CompressFile(string ArchiveName, string SrcFileName, string Pwd)
        {
            try
            {
                SevenZipCompressor szc = new SevenZipCompressor();
                szc.ArchiveFormat = OutArchiveFormat.SevenZip;                
                ArchiveName = currentPath + ArchiveName;
                SrcFileName = currentPath + SrcFileName;
                //szc.CompressFiles(ArchiveName, SrcFileName);
                szc.CompressFilesEncrypted(ArchiveName, Pwd, SrcFileName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="fileList">string[] 需压缩文件列表</param>
        /// <param name="zipedFilePath">压缩后的压缩文件路径，如: d:\test\aa.zip</param>
        /// <returns>true or false</returns>
        public static bool CompressFile(string[] fileList, string zipedFilePath)
        {
            bool result = true;
            try
            {
                for (int i = 0; i < fileList.Length; i++)
                {
                    if (!File.Exists(fileList[i]))
                    {
                        result = false;
                        throw new Exception("需压缩文件不存在，退出压缩！");
                    }
                }

                string dir = Path.GetDirectoryName(zipedFilePath);
                if (!dir.EndsWith(@"\")) dir = dir + @"\";
                if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

                SevenZipCompressor cmp = new SevenZipCompressor
                {
                    ArchiveFormat = OutArchiveFormat.Zip,
                    CompressionLevel = CompressionLevel.High,
                    CompressionMethod = SevenZip.CompressionMethod.BZip2,
                    CompressionMode = CompressionMode.Create
                };

                cmp.CompressFiles(zipedFilePath, fileList);
            }
            catch (Exception ee)
            {
                result = false;
                Logger.Record(String.Format("CompressFile异常: {0}~{1}~{2}", ee.Message, ee.Source, ee.StackTrace), LogType.Error);
            }
            return result;
        }

        /// <summary>
        /// 解压缩文件
        /// </summary>
        /// <param name="unZipFile">需解压文件位置， 如: d:\aa.zip</param>
        /// <param name="unZipTo">解压至，如: d:\aa</param>
        /// <returns></returns>
        public static bool ExtractorFileWTF(string unZipFile, string unZipTo)
        {
            bool result = true;
            try
            {
                if (!File.Exists(unZipFile))
                {
                    result = false;
                    throw new Exception("需解压文件不存在，退出解压缩！");
                }

                using (SevenZipExtractor unzip = new SevenZipExtractor(unZipFile))
                {
                    for (int i = 0; i < unzip.ArchiveFileData.Count; i++)
                    {
                        unzip.ExtractFiles(unZipTo, unzip.ArchiveFileData[i].Index);
                    }
                }
            }
            catch (Exception ee)
            {
                result = false;
                Logger.Record(String.Format("ExtractorFile异常: {0}~{1}~{2}", ee.Message, ee.Source, ee.StackTrace), LogType.Error);
            }
            return result;
        }

        public static void ExtractorFile(string ArchiveName, string Pwd)
        {
            try
            {
                ArchiveName = currentPath + ArchiveName;
                using(SevenZipExtractor sze = new SevenZipExtractor(ArchiveName,Pwd))
                {
                    for(int i=0;i<sze.ArchiveFileData.Count;i++)
                    {
                        sze.ExtractFiles(currentPath,sze.ArchiveFileData[i].Index);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool ExtractorFileEx(string FilePath, string ExtractPath, string Pwd)
        {
            try
            {
                using (SevenZipExtractor sze = new SevenZipExtractor(FilePath, Pwd))
                {
                    for (int i = 0; i < sze.ArchiveFileData.Count; i++)
                    {
                        sze.ExtractFiles(ExtractPath, sze.ArchiveFileData[i].Index);
                    }
                    return true;
                }                
            }
            catch (Exception ee)
            {
                Logger.Record(String.Format("ExtractorFileEx异常: {0}~{1}~{2}", ee.Message, ee.Source, ee.StackTrace), LogType.Error);
                return false;
                throw ee;                
            }
        }


    }
}
